﻿


// Author:					Doan Quoc Tuan
// Created:					2012-6-19
// Last Modified:			2012-6-19
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using SMSPanasonic.Data;

namespace SMSPanasonic.Business
{

    public class SmsRouter
    {

        #region Constructors

        public SmsRouter()
        { }


        public SmsRouter(
            SqlInt32 routerID)
        {
            GetSmsRouter(
                routerID);
        }

        #endregion

        #region Private Properties

        private SqlInt32 _routerID = SqlInt32.Null;
        private SqlString _routerName = SqlString.Null;
        private SqlString _routerDescription = SqlString.Null;
        private SqlString _typeOfObject = SqlString.Null;
        private SqlString _routerURL = SqlString.Null;
        private SqlString _routerURL2 = SqlString.Null;
        private SqlString _userName = SqlString.Null;
        private SqlString _password = SqlString.Null;
        private SqlString _routerType = SqlString.Null;
        private SqlString _publicKey = SqlString.Null;
        private SqlString _publicPrivateKey = SqlString.Null;
        private SqlBoolean _enabled = SqlBoolean.Null;
        private SqlInt32 _numOfThread = SqlInt32.Null;

        #endregion

        #region Public Properties

        public SqlInt32 RouterID
        {
            get { return _routerID; }
            set { _routerID = value; }
        }
        public SqlString RouterName
        {
            get { return _routerName; }
            set { _routerName = value; }
        }
        public SqlString RouterDescription
        {
            get { return _routerDescription; }
            set { _routerDescription = value; }
        }
        public SqlString TypeOfObject
        {
            get { return _typeOfObject; }
            set { _typeOfObject = value; }
        }
        public SqlString RouterURL
        {
            get { return _routerURL; }
            set { _routerURL = value; }
        }
        public SqlString RouterURL2
        {
            get { return _routerURL2; }
            set { _routerURL2 = value; }
        }
        public SqlString UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }
        public SqlString Password
        {
            get { return _password; }
            set { _password = value; }
        }
        public SqlString RouterType
        {
            get { return _routerType; }
            set { _routerType = value; }
        }
        public SqlString PublicKey
        {
            get { return _publicKey; }
            set { _publicKey = value; }
        }
        public SqlString PublicPrivateKey
        {
            get { return _publicPrivateKey; }
            set { _publicPrivateKey = value; }
        }
        public SqlBoolean Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }
        public SqlInt32 NumOfThread
        {
            get { return _numOfThread; }
            set { _numOfThread = value; }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of SmsRouter.
        /// </summary>
        /// <param name="routerID"> routerID </param>
        private void GetSmsRouter(
            SqlInt32 routerID)
        {
            using (IDataReader reader = DBSmsRouter.GetOne(
                routerID))
            {
                PopulateFromReader(reader);
            }

        }


        private void PopulateFromReader(IDataReader reader)
        {
            if (reader.Read())
            {
                if (!reader.IsDBNull(0)) this._routerID = reader.GetInt32(0);
                if (!reader.IsDBNull(1)) this._routerName = reader.GetString(1);
                if (!reader.IsDBNull(2)) this._routerDescription = reader.GetString(2);
                if (!reader.IsDBNull(3)) this._typeOfObject = reader.GetString(3);
                if (!reader.IsDBNull(4)) this._routerURL = reader.GetString(4);
                if (!reader.IsDBNull(5)) this._routerURL2 = reader.GetString(5);
                if (!reader.IsDBNull(6)) this._userName = reader.GetString(6);
                if (!reader.IsDBNull(7)) this._password = reader.GetString(7);
                if (!reader.IsDBNull(8)) this._routerType = reader.GetString(8);
                if (!reader.IsDBNull(9)) this._publicKey = reader.GetString(9);
                if (!reader.IsDBNull(10)) this._publicPrivateKey = reader.GetString(10);
                if (!reader.IsDBNull(11)) this._enabled = reader.GetBoolean(11);
                if (!reader.IsDBNull(12)) this._numOfThread = reader.GetInt32(12);

            }

        }

        /// <summary>
        /// Persists a new instance of SmsRouter. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create()
        {
            int newID = 0;

            newID = DBSmsRouter.Create(
                this._routerName,
                this._routerDescription,
                this._typeOfObject,
                this._routerURL,
                this._routerURL2,
                this._userName,
                this._password,
                this._routerType,
                this._publicKey,
                this._publicPrivateKey,
                this._enabled,
                this._numOfThread);

            this._routerID = newID;

            return (newID > 0);

        }


        /// <summary>
        /// Updates this instance of SmsRouter. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update()
        {

            return DBSmsRouter.Update(
                this._routerID,
                this._routerName,
                this._routerDescription,
                this._typeOfObject,
                this._routerURL,
                this._routerURL2,
                this._userName,
                this._password,
                this._routerType,
                this._publicKey,
                this._publicPrivateKey,
                this._enabled,
                this._numOfThread);

        }





        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance of SmsRouter. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save()
        {
            if (!this._routerID.IsNull)
            {
                return Update();
            }
            else
            {
                return Create();
            }

        }




        #endregion

        #region Static Methods

        /// <summary>
        /// Deletes an instance of SmsRouter. Returns true on success.
        /// </summary>
        /// <param name="routerID"> routerID </param>
        /// <returns>bool</returns>
        public static bool Delete(
            SqlInt32 routerID)
        {
            return DBSmsRouter.Delete(
                routerID);
        }


        /// <summary>
        /// Gets a count of SmsRouter. 
        /// </summary>
        public static int GetCount(string sWhereCondition)
        {
            return DBSmsRouter.GetCount(sWhereCondition);
        }

        private static List<SmsRouter> LoadListFromReader(IDataReader reader)
        {
            List<SmsRouter> _smsRouterList = new List<SmsRouter>();
            try
            {
                while (reader.Read())
                {
                    SmsRouter _smsRouter = new SmsRouter();
                    if (!reader.IsDBNull(0)) _smsRouter._routerID = reader.GetInt32(0);

                    if (!reader.IsDBNull(1)) _smsRouter._routerName = reader.GetString(1);

                    if (!reader.IsDBNull(2)) _smsRouter._routerDescription = reader.GetString(2);

                    if (!reader.IsDBNull(3)) _smsRouter._typeOfObject = reader.GetString(3);

                    if (!reader.IsDBNull(4)) _smsRouter._routerURL = reader.GetString(4);

                    if (!reader.IsDBNull(5)) _smsRouter._routerURL2 = reader.GetString(5);

                    if (!reader.IsDBNull(6)) _smsRouter._userName = reader.GetString(6);

                    if (!reader.IsDBNull(7)) _smsRouter._password = reader.GetString(7);

                    if (!reader.IsDBNull(8)) _smsRouter._routerType = reader.GetString(8);

                    if (!reader.IsDBNull(9)) _smsRouter._publicKey = reader.GetString(9);

                    if (!reader.IsDBNull(10)) _smsRouter._publicPrivateKey = reader.GetString(10);

                    if (!reader.IsDBNull(11)) _smsRouter._enabled = reader.GetBoolean(11);

                    if (!reader.IsDBNull(12)) _smsRouter._numOfThread = reader.GetInt32(12);

                    _smsRouterList.Add(_smsRouter);

                }
            }
            finally
            {
                reader.Close();
            }

            return _smsRouterList;

        }

        /// <summary>
        /// Gets an IList with all instances of SmsRouter.
        /// </summary>
        public static List<SmsRouter> GetAll()
        {
            IDataReader reader = DBSmsRouter.GetAll();
            return LoadListFromReader(reader);

        }

        /// <summary>
        /// Gets an IList with page of instances of SmsRouter.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static List<SmsRouter> GetPage(string sWhereCondition, string sOrderBy, int pageNumber, int pageSize, out int totalPages)
        {
            totalPages = 1;
            IDataReader reader = DBSmsRouter.GetPage(sWhereCondition, sOrderBy, pageNumber, pageSize, out totalPages);
            return LoadListFromReader(reader);
        }



        #endregion

        #region Comparison Methods

        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByRouterID(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.RouterID.CompareTo(_smsRouter2.RouterID);
        }
        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByRouterName(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.RouterName.CompareTo(_smsRouter2.RouterName);
        }
        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByRouterDescription(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.RouterDescription.CompareTo(_smsRouter2.RouterDescription);
        }
        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByTypeOfObject(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.TypeOfObject.CompareTo(_smsRouter2.TypeOfObject);
        }
        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByRouterURL(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.RouterURL.CompareTo(_smsRouter2.RouterURL);
        }
        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByRouterURL2(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.RouterURL2.CompareTo(_smsRouter2.RouterURL2);
        }
        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByUserName(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.UserName.CompareTo(_smsRouter2.UserName);
        }
        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByPassword(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.Password.CompareTo(_smsRouter2.Password);
        }
        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByRouterType(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.RouterType.CompareTo(_smsRouter2.RouterType);
        }
        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByPublicKey(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.PublicKey.CompareTo(_smsRouter2.PublicKey);
        }
        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByPublicPrivateKey(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.PublicPrivateKey.CompareTo(_smsRouter2.PublicPrivateKey);
        }
        /// <summary>
        /// Compares 2 instances of SmsRouter.
        /// </summary>
        public static int CompareByNumOfThread(SmsRouter _smsRouter1, SmsRouter _smsRouter2)
        {
            return _smsRouter1.NumOfThread.CompareTo(_smsRouter2.NumOfThread);
        }

        #endregion


    }

}





